//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
var request = require("../../utils/request.js");

// const provinces = []
// const citys = []
// const countys = []


Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    currentIndex: 0,
    isclick: false,
    istap: false,
    current: 0,
    region: '',
    customItem: '',
    value: '',
    provinces: '',
    citys: '',
    countys: '',
    provin_id: '',
    citys_id: '',
    country_id: '',
    status:''

  },
  onLoad: function () {
    var that = this;
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    that.setData({
      logs: (wx.getStorageSync('logs') || []).map(function (log) {
        return util.formatTime(new Date(log))
      })
    })
    that.getData();
    that.shoolLis();

  },
  // 轮播图
  getData: function () {
    var that = this;

    var url = request.dataUrl + '/api/index/carousel_list';
    var params = {
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      // console.log(res)

      that.setData({
        banner: res.msg
      })

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  // 获取学校、专业联系方式
  getSchoolCont: function (e) {
    var that = this;

    var url = request.dataUrl + '/api/index/get_information';
    var params = {
      status: that.data.status
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
       console.log(res)
      that.setData({
        shools: res.msg
      })

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  //获取学校列表
  shoolLis: function () {
    var that = this;
    console.log(that.data.current)
    var url = request.dataUrl + '/api/index/school_list';
    var params = {
      type: that.data.current,
      province: '',
      city: '',
      county: ''
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          profession: res.msg.profession,
          school: res.msg.school
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  //获取省份
  getProvinces: function () {
    var that = this;
    console.log(that.data.current)
    var url = request.dataUrl + '/api/index/get_province';
    var params = {
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          provinces: res.msg,
          provin_id: res.msg[0].region_id
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  //获取城市
  getcitys: function () {
    var that = this;
    console.log(that.data.current)
    var url = request.dataUrl + '/api/index/get_city';    
    var params = {
      parent_id: that.data.provin_id
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          citys: res.msg,
          citys_id: res.msg[0].region_id
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  //获取县、区
  getcountys: function () {
    var that = this;
    console.log(that.data.current)
    var url = request.dataUrl + '/api/index/get_county';
    var params = {
      parent_id: that.data.citys_id
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          countys: res.msg,
          country_id: res.msg[0].region_id
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  // 获取单个省份的学校，专业列表
  getProShool: function () {
    var that = this;
    
    var url = request.dataUrl + '/api/index/school_list';
    var params = {
      type: that.data.current,
      province: that.data.provin_id,
      city: '',
      county: ''
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          province_profession: res.msg.profession,
          province_school: res.msg.school
        })
      }else{
        that.setData({
          province_profession: '',
          province_school: ''
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  // 获取单个城市的学校，专业列表
  getCityShool: function () {
    var that = this;
    var url = request.dataUrl + '/api/index/school_list';
    var params = {
      type: that.data.current,
      province: that.data.provin_id,
      city: that.data.citys_id,
      county: ''
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          city_profession: res.msg.profession,
          city_school: res.msg.school
        })
      } else {
        that.setData({
          city_profession: '',
          city_school: ''
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  // 获取单个乡、区的学校，专业列表
  getCountryShool: function () {
    var that = this;
    var url = request.dataUrl + '/api/index/school_list';
    var params = {
      type: that.data.current,
      province: that.data.provin_id,
      city: that.data.citys_id,
      county: that.data.country_id
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          county_profession: res.msg.profession,
          county_school: res.msg.school
        })
      } else {
        that.setData({
          county_profession: '',
          county_school: ''
        })
      }

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  //获取省份
  bindChange(e) {
    var that = this;
    const val = e.detail.value
    if (that.data.currentIndex == 1) {
      console.log(that.data.provinces[val[0]])
      that.setData({
        provin_id: that.data.provinces[val[0]].region_id,
      })
    } else if (that.data.currentIndex == 2) {
      console.log(that.data.citys[val[0]])
      that.setData({
        citys_id: that.data.citys[val[0]].region_id,
      })
    } else if (that.data.currentIndex == 3) {
      console.log(that.data.countys[val[0]])
      that.setData({
        country_id: that.data.countys[val[0]].region_id,
      })
    }

  },
  // 确认当前的省份
  opened(e) {
    var that = this;
    if (that.data.currentIndex == 1) {
      that.getProShool();
    } else if (that.data.currentIndex == 2) {
      that.getCityShool();
    } else if (that.data.currentIndex == 3) {
      that.getCountryShool();
    }

    that.setData({
      maskVisual: 'hidden',
      condition: !this.data.condition
    })

  },
  // 取消
  cancel(e) {
    this.setData({
      maskVisual: 'hidden',
      condition: !this.data.condition
    })
  },
  // 院校切换
  changTasp(e) {
    var that = this;
    that.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  // 搜索
  tosearch(e) {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 事件 好的学校
  goodSchool(e) {
    var that = this;
    console.log(e)
    that.setData({
      isclick: true,
      status: e.currentTarget.dataset.status
    })
    that.getSchoolCont();
  },
  // 蒙版
  window01(e) {
    var that = this;
    that.setData({
      isclick: false
    })
  },
  // 拨打电话
  makePhoneCall01(e) {
    wx.makePhoneCall({
      phoneNumber: '13526868609',
    })

  },
  // 事件 专业
  goodMajor(e) {
    var that = this;
    that.setData({
      isclick: true,
      status:e.currentTarget.dataset.status
    })
    that.getSchoolCont();
  },
  // 蒙版
  window02(e) {
    var that = this;
    that.setData({
      istap: false
    })
  },
  // 拨打电话
  makePhoneCall02(e) {
    wx.makePhoneCall({
      phoneNumber: '13526868609',
    })

  },
  // 院校切换
  navtab(e) {
    var that = this;
    that.setData({
      current: e.currentTarget.dataset.index
    })
    that.shoolLis();
  },
  navDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: '../navDetail/navDetail?title=' + e.currentTarget.dataset.title + '&id=' +e.currentTarget.dataset.id,
    })
  },
  // 联动切换
  areaShow: function (e) {
    var that = this;
    that.setData({
      currentIndex: e.currentTarget.dataset.index
    })

    if (e.currentTarget.dataset.index == 1) {
      that.getProvinces();
    } else if (e.currentTarget.dataset.index == 2) {
      that.getcitys();
    } else if (e.currentTarget.dataset.index == 3) {
      that.getcountys();
    }

    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
    animation.translateY(-285).step();
    this.setData({
      animationData: this.animation.export(),
      maskVisual: 'show',
      condition: !this.data.condition
    });
  },
  cascadeDismiss: function () {
    this.animation.translateY(285).step();
    this.setData({
      animationData: this.animation.export(),
      maskVisual: 'hidden',
      condition: !this.data.condition
    });
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
